/**  
 * All rights Reserved, Designed By <a href="www.itttq.com" target="_blank">www.itttq.com</a>
 * @date:   2018年1月2日 下午4:42:56
 * @Copyright: 2018 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */  
package com.itttq.gshop.pbpm.provider;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.jdbc.SQL;

import com.itttq.gshop.domain.po.SmsPlatformPo;
import com.itttq.gshop.pbpm.cmpt.mybatis.CustomerSQL;

/**   
 * @ClassName:  SmsPlatformPoSqlProvider
 * @author: 迷蝶
 * @date:   2018年1月2日 下午4:42:56 
 * @Description: TODO
 *     
 * @Copyright: 2018 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */
public class SmsPlatformSqlProvider {
	public static final String TABLE_NAME = "sms_platform";

	/**
	 * 查找的SQL语句组装<br>
	 * 约定：分页两个参数为：start - 起始位置； size- 每页数量
	 * 
	 * @param smsPlatform
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String findSmsPlatforms(final SmsPlatformPo smsPlatform) {
		return new CustomerSQL() {{
			SELECT("api_url,app_key,app_secret,create_time,creator_id,description,id,name,signature,update_time,updater_id ");
			FROM(TABLE_NAME);
			
			if (StringUtils.isNotBlank(smsPlatform.getApiUrl())) {
				WHERE("api_url=#{apiUrl}");
			}
			if (StringUtils.isNotBlank(smsPlatform.getAppKey())) {
				WHERE("app_key=#{appKey}");
			}
			if (StringUtils.isNotBlank(smsPlatform.getAppSecret())) {
				WHERE("app_secret=#{appSecret}");
			}
			if (null != smsPlatform.getCreatorId()) {
				WHERE("creator_id=#{creatorId}");
			}
			if (StringUtils.isNotBlank(smsPlatform.getDescription())) {
				WHERE("description=#{description}");
			}
			if (StringUtils.isNotBlank(smsPlatform.getName())) {
				WHERE("name=#{name}");
			}
			if (StringUtils.isNotBlank(smsPlatform.getSignature())) {
				WHERE("signature=#{signature}");
			}
			if (null != smsPlatform.getUpdaterId()) {
				WHERE("updater_id=#{updaterId}");
			}
			if(StringUtils.isNotBlank(smsPlatform.getSortField())) {
				ORDER_BY(smsPlatform.getSortField() + (StringUtils.isNotBlank(smsPlatform.getSortMethod()) ? "desc" : smsPlatform.getSortField()));
			} else {
				ORDER_BY("id desc");
			}
			if(null != smsPlatform.getStart() && null != smsPlatform.getSize()) {
				LIMIT(smsPlatform.getStart(), smsPlatform.getSize());
			}
		}}.toString();
	}
	
	/**
	 * 查找记录总数的SQL语句组装
	 * 
	 * @param smsPlatform
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String findSmsPlatformCount(final SmsPlatformPo smsPlatform) {
		return new SQL() {{
			SELECT("count(*)");
			FROM(TABLE_NAME);
			if (StringUtils.isNotBlank(smsPlatform.getApiUrl())) {
				WHERE("api_url=#{apiUrl}");
			}
			if (StringUtils.isNotBlank(smsPlatform.getAppKey())) {
				WHERE("app_key=#{appKey}");
			}
			if (StringUtils.isNotBlank(smsPlatform.getAppSecret())) {
				WHERE("app_secret=#{appSecret}");
			}
			if (null != smsPlatform.getCreatorId()) {
				WHERE("creator_id=#{creatorId}");
			}
			if (StringUtils.isNotBlank(smsPlatform.getDescription())) {
				WHERE("description=#{description}");
			}
			if (StringUtils.isNotBlank(smsPlatform.getName())) {
				WHERE("name=#{name}");
			}
			if (StringUtils.isNotBlank(smsPlatform.getSignature())) {
				WHERE("signature=#{signature}");
			}
			if (null != smsPlatform.getUpdaterId()) {
				WHERE("updater_id=#{updaterId}");
			}
		}}.toString();
	}
	
	/**
	 * 根据ID查找的SQL语句组装
	 * 
	 * @param id
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String findSmsPlatformById(final Integer id) {
		return new SQL() {{
			SELECT("*");
			FROM(TABLE_NAME);
			WHERE("id = #{id}");
		}}.toString();
	}
	
	/**
	 * 增加的SQL语句组装
	 * 
	 * @param smsPlatform
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String addSmsPlatform(final SmsPlatformPo smsPlatform) {
		return new SQL() {{
			INSERT_INTO(TABLE_NAME);
			if (StringUtils.isNotBlank(smsPlatform.getApiUrl())) {
				VALUES("api_url","#{apiUrl}");
			}
			if (StringUtils.isNotBlank(smsPlatform.getAppKey())) {
				VALUES("app_key","#{appKey}");
			}
			if (StringUtils.isNotBlank(smsPlatform.getAppSecret())) {
				VALUES("app_secret","#{appSecret}");
			}
			if (null != smsPlatform.getCreatorId()) {
				VALUES("creator_id","#{creatorId}");
			}
			if (StringUtils.isNotBlank(smsPlatform.getDescription())) {
				VALUES("description","#{description}");
			}
			if (StringUtils.isNotBlank(smsPlatform.getName())) {
				VALUES("name","#{name}");
			}
			if (StringUtils.isNotBlank(smsPlatform.getSignature())) {
				VALUES("signature","#{signature}");
			}
			if (null != smsPlatform.getUpdaterId()) {
				VALUES("updater_id","#{updaterId}");
			}
		}}.toString();
	}

	/**
	 * 修改的SQL语句组装
	 * 
	 * @param smsPlatform
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String updateSmsPlatform(final SmsPlatformPo smsPlatform) {
		return new SQL() {{
			UPDATE(TABLE_NAME);
			
			if (StringUtils.isNotBlank(smsPlatform.getApiUrl())) {
				SET("api_url=#{apiUrl}");
			}
			if (StringUtils.isNotBlank(smsPlatform.getAppKey())) {
				SET("app_key=#{appKey}");
			}
			if (StringUtils.isNotBlank(smsPlatform.getAppSecret())) {
				SET("app_secret=#{appSecret}");
			}
			if (null != smsPlatform.getCreatorId()) {
				SET("creator_id=#{creatorId}");
			}
			if (StringUtils.isNotBlank(smsPlatform.getDescription())) {
				SET("description=#{description}");
			}
			if (StringUtils.isNotBlank(smsPlatform.getName())) {
				SET("name=#{name}");
			}
			if (StringUtils.isNotBlank(smsPlatform.getSignature())) {
				SET("signature=#{signature}");
			}
			if (null != smsPlatform.getUpdaterId()) {
				SET("updater_id=#{updaterId}");
			}
			WHERE("id = #{id}");
		}}.toString();
	}
	
	/**
	 * 根据ID删除的SQL语句组装
	 * 
	 * @param id
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String delSmsPlatform(final Integer id) {
		return new SQL() {{
			DELETE_FROM(TABLE_NAME);
			WHERE("id = #{id}");
		}}.toString();
	}
}
